home *** CD-ROM | disk | FTP | other *** search
/ Celestin Apprentice 4 / Apprentice-Release4.iso / Demos / Codeworks 0.94b3 / Codeworks® WWW Demo Doc. / Scripting Manual.doc / Scripting Manual.doc.rsrc / TEXT_152.txt < prev    next >
Encoding:
Text File  |  1995-05-01  |  7.9 KB  |  148 lines

  1. Groups
  2.  
  3.     Groups are powerful objects.  With them you can do things that would be much harder in other languages.  A group is a collection of other objects.  Any object whatsoever can be in a group: a number, a string, the value true, a cash-register, or any object you create.  Even a group can be part of another group.  The items in a group are its elements.
  4.  
  5.     There are many different ways of working with groups, because there are many different ways of working with groups of objects.  Sometimes you want to treat them simply as a collection of related objects.  Sometimes the objects are kept in a specific order.  In other languages there are stacks, queues, lists, and arrays.  In Glyphic Script, there is only group, which serves them all.
  6.  
  7.  
  8. Basic Operations
  9.  
  10. g add e    add e to the group g
  11. g remove e    remove the first occurrence of e, if it‚Äôs there, from g
  12. g.size    return the number of objects in g
  13.  
  14.     These are the basic messages for handling a group.  Use them when you don‚Äôt particularly care about the order of items in a group.  You can follow the effect of the following expressions by opening up a workspace, executing the first expression, then opening up the workspace variable a to view it.  Then, as you execute each expression in turn, you can see the effect on the group:
  15.  
  16. Expression    Contents of a    Comments
  17. a := new group.    empty    creates a new, empty group
  18. a add "cat".    cat    adds a string
  19. a add "bird".    cat, bird    adds another
  20. a add 123.    cat, bird, 123    adds a number
  21. a remove "bird".    cat, 123    removes a string, gap closes
  22. a add"cat".    cat, 123, cat    adds a second copy of a string
  23. a remove "cat".    123, cat    removes the first one it finds
  24. a remove 456.    123, cat    no effect, as 456 isn‚Äôt there
  25. a.size.        resulting size is 2
  26.     
  27.  
  28.  
  29.  
  30.  
  31. List Operations
  32.     A group can be treated as an ordered list.  Each element in the list has an index.  The index runs from one to the size of the group.  If you insert or remove a given item, all the items shift over.
  33.  
  34. g insert e after i
  35. g insert e before i    insert e into the group after or before index i
  36. g delete i        delete i-th item of the group
  37. g place e after f
  38. g place e before f
  39.  insert e into the group after or before the first occurrence of f
  40. g @ i        returns the i-th item of the group
  41. g @ i := e        replaces the i-th item of the group with e
  42. g index of e
  43.  returns the index of the first occurrence of e, if there aren‚Äôt any then ???
  44.  
  45. Expression                                   Contents of a
  46. a := new group.                           empty
  47. a insert "cat" before 1.            cat
  48. a insert "bird" after 1.            cat, bird
  49. a place 123 before "bird".       cat, 123, bird    bird is now item 3
  50. a delete 1.                                  123, bird    bird is back to item 2
  51. a place "cat" after 123.           123, cat, bird
  52. a delete 3.                                  123, cat
  53. a @ 2 := "dog".                             123, dog    replaces cat with dog
  54. a @ 1.                                           123, dog    results in 123
  55. a index of "dog"                          123, dog    results in 2
  56. a index of "cat"                           123, dog    results in ???
  57.     
  58.  
  59.     
  60. Queue Operations
  61.     Many tasks involve keeping track of objects in a queue.  A queue is a computer science term for lists where items are added or removed only from the ends.  For example: a cash-register tape is a kind of queue: new entries are only added to the end.  A movie ticket line is a kind of queue: new people get on the end of the queue, and as tickets are sold, people leave the front.
  62.  
  63.     While you could use the list operations above, there is a set of queue messages to make queue management easier.  Queues have two ends, the front and back.  The object at the front of the queue is first, and the object at the back is last.  Pushing an object onto a queue means to add it from an end.  Popping it means to remove it from an end.  When you pop an object, the result of the operation is the object just removed.
  64.  
  65. g push e first add e to the front of the queue
  66. g push e last add e to the end of the queue
  67. g pop first remove and return the front of the queue
  68. g pop last remove and return the end of the queue
  69. g.first    return the front of the queue (without removing it)
  70. g.last    return the end of the queue (without removing it)
  71.  
  72.     Example Sequence:
  73.     Expression                          Contents of a
  74.     a := new group.               empty
  75.     a push "sue" last.          sue    add sue
  76.     a push "bob".                    sue, bob    last is the default
  77.     a push "jill" first.        jill, sue, bob    jill gets ahead in line
  78.     a.first                                jill, sue, bob    Ô¨Å jill
  79.     a.last                                  jill, sue, bob    Ô¨Å bob
  80.     a pop last.                       jill, sue    Ô¨Å bob, take from end
  81.     a pop.                               jill     Ô¨Å sue, last is default
  82.     a pop first.                    empty  Ô¨Å jill
  83.     
  84.         
  85.  
  86. Set operations
  87.     In all the above operations, a group could hold an object more than once.  If you add "cat" to a group three times, there will be three "cats" in the group.  Similarly, if there were two numbers in the group both 100, then removing 100, would only remove one of them.  Sometimes it is desirable to only add an object to a group if it is not there, and remove every copy of it if it is.  These messages do this:
  88.  
  89. g include e    add e to the group if it isn‚Äôt there
  90. g exclude e    remove e from the group, multiple times if needed
  91.     
  92.     If a group doesn‚Äôt have any element more than once, then the group can be thought of as a set.  You can perform set operations on two groups, producing a third group from them:
  93.     
  94. g union f    a new group with every element in g and f
  95. g intersect f    a new group with only elements that are in both g and f
  96. g difference f    a new group with every element in g that is not in f
  97.  
  98.     Example Sequence:
  99. Expression                     Contents of a/b/result
  100. a := new group.             empty
  101. a add "sue".                    sue    start off with a group
  102. a add "bob".                   sue, bob
  103. a add "sue".                   sue, bob, sue
  104. a include "bob".           sue, bob, sue    no change, already has bob
  105. a exclude "sue".           bob    removes both sues
  106. a include "jill".           bob, jill    adds jill
  107. b := new group.             empty 
  108. b include "bob".           bob
  109. b include "sue".           bob, sue
  110. a union b                       Ô¨Å bob, jill, sue    a new group
  111. a intersect b                Ô¨Å bob        only item in both
  112. a difference b              Ô¨Å jill        item in a, not in b
  113.     
  114.         
  115. General Operations
  116.     These messages are applicable to groups in almost all contexts. 
  117.     
  118. g.empty    returns true if g is empty
  119. g from i
  120. g from i to j
  121. g from i for j
  122.  returns a new group that contains the elements of g starting at index i.  With no extra arguments, it takes all the elements to the end.  With the to argument, it takes elements up to index j.  With the for argument, it takes the next j elements.
  123. g & f
  124.  returns a new group which has all the elements of g followed by all the elements of f
  125. g has e    returns true if g has the element e
  126. g count e    returns the number of times e is in g
  127. choose g    returns a random element of g
  128.  
  129.  
  130.  
  131.     These messages take blocks of code to control their operation.
  132.  
  133. g transform by [ $ element e, index i. ‚Ķ ]
  134.     Returns a new group that contains the results of executing the block for each element in g.
  135.  
  136. g reject by [ $ - e. ‚Ķ ]
  137.     Executes the block for each element in g and removes the element if the result of the block is true. 
  138.  
  139. g sort
  140. g sort by [ $ - a, - b. ‚Ķ ]
  141. g sort by-value [ $ - a. ‚Ķ ]
  142.  
  143.     These messages sort the group.  In the first form, the elements are compared with the < operator.  This works for numbers and strings, but most other objects don‚Äôt understand the < operator.  In these cases, you have two choices: the first form, with the by argument, you supply a block that compares the two block arguments and returns true if the first should sort before the second.  The second form, with the by-value argument, you supply a block that returns for each element a value that can be compared with the < operator.  The first form is more efficient than the second.
  144.  
  145. for g do [ $ element e, index i. ‚Ķ ]
  146. for g reverse do [ $ element e, index i. ‚Ķ]
  147.     This is just the for statement discussed earlier in this manual.
  148.